<?php
/**
 * Created by PhpStorm.
 * User: SYSTEM
 * Date: 2016/6/28
 * Time: 3:06
 * 商品控制类
 */
namespace Admin\Controller;

use Admin\Model\AuthGroupModel;
use Think\Page;

/**
 * 后台内容控制器
 * @author huajie <banhuajie@163.com>
 */
class GoodController extends AdminController
{
    public function panicproduct(){

        $good_id = $_GET["good_id"];
        if($good_id){
            $where['id'] = $good_id;
        }
        $sub_title = $_GET["sub_title"];
        if($sub_title){
            $where['sub_title'] = $sub_title;
        }
        $type = $_GET["type"];
        if($type){
            $where['type'] = $type;
        }
        $goodinfo = M("Good")->where($where)->order("create_time desc")->select();
        $goodinvest = array();
        $investlist = M("Invest")->where(array("status"=>"1"))->select();
        if(is_array($investlist)){
            foreach($investlist as $key=>$val){
                $good_info = json_decode($val['good_info'],true);
                if(!is_array($good_info)){
                    $good_info = json_decode($good_info,true);
                }
                foreach($good_info as $k=>$v){
                    $goodinvest[$v["good_id"]] += $v['count'];
                }
            }
        }
        $ActiveGoods = M("ActiveGoods")->select();
        $inactivegoods = array();
        if(is_array($ActiveGoods)){
            foreach($ActiveGoods as $key=>$val){
                $inactivegoods[$val['goods_id']] = '1';
            }
        }
        $GroupGoods = M("GroupGoods")->select();
        $ingroupgoods = array();
        if(is_array($GroupGoods)){
            foreach($GroupGoods as $key=>$val){
                $ingroupgoods[$val['goods_id']] = '1';
            }
        }
        $list = array();
        foreach($goodinfo as $key=>$val){
            $val['investcount'] = $goodinvest[$val['id']];
            if($val['status']==1){
                $val['status'] = "正常";
            }else{
                $val['status'] = "下架";
            }
            if($inactivegoods[$val['id']] == 1){
                $val['activename'] = "砍价商品";
            }
            if($ingroupgoods[$val['id']] == 1){
                $val['activename'] = "团购商品";
            }
            $list[$key] = $val;
        }
        $this->assign('_list', $list);
        $this->display();
    }

    public function panicadd()
    {
        $fileType = I('file_type', '', '/^\d+$/');
        $id = $_GET['id'];
        $this->assign("categoryinfo",0);
        $this->assign("priceinfo",0);
        $uid = $_SESSION['onethink_admin']['user_auth']['uid'];
        if($uid != '1'){
            $a['supplier'] = $uid;
            $this->assign("goodinfo",$a);
        }
        if($id){
            $goodinfo = M("Good")->where(array("id"=>$id))->find();
            $freeprovince = $goodinfo['freeprovince']?$goodinfo['freeprovince']:0;
            $this->assign("freeprovince",$freeprovince);

            if($goodinfo['img_path']){
                $goodinfo['img_path_p'] = json_encode(explode(",",$goodinfo['img_path']));
            }else{
                $goodinfo['img_path_p'] = 0;
            }

            $this->assign("goodinfo",$goodinfo);
        }
        !$fileType && $this->error('参数错误');
        $this->assign('_title', '新增文件');
        $this->assign('fileLimit', file_limit($fileType));
        $this->assign('file_type', $fileType);

        $this->assign('file_type_name', $fileTypename);
        $this->display();
    }
    //删除商品
    public function deletegood(){
        $id = $_GET['id'];
        if(empty($id)){
            $this->error('参数错误');
        }else{
            $goodinfo = M("Good")->where(array("id"=>$id))->delete();
            $this->success('删除成功');
        }
    }
    public function addGood()
    {

        $data = array();
        $data['type'] = $_POST['type'];
        $data['title'] = $_POST['title'];
        $data['sub_title'] = $_POST['sub_title'];
        $data['info'] = $_POST['info'];

        $data['status'] = '1';
        $data['create_time'] = date("Y-m-d H:i:s");
        $data['purchase_num'] = $_POST['purchase_num'];
        $data['production_time'] = $_POST['production_time'];
        $data['area'] = $_POST['city'];
        $data['city'] = $_POST['area'];
        $data['province'] = $_POST['province'];
        if(empty($_POST['img_path'])){
            $this->error('请上传图片');
        }
        $img_path = implode(",",array_filter(explode(",",$_POST['img_path'])));
        $data['img_path'] = $img_path;
        $data['original_price'] = $_POST['original_price'];
        $data['discount_price'] = $_POST['discount_price'];
        $data['stock'] = $_POST['stock'];
        $data['appointment'] = $_POST['appointment'];
        $data['appointment_node'] = $_POST['appointment_node'];
        $data['end_time'] = $_POST['end_time'];
        $data['freeprovince'] = implode(",",$_POST['freeprovince']);
        $data['postage'] = $_POST['postage'];
        $data['supplier'] = $_POST['supplier'];
        $data['re_purchase']=$_POST['re_purchase'];
        $data['discount_price_tow']=$_POST['discount_price_tow'];
        $data['discount_price_three']=$_POST['discount_price_three'];
        $data['discount_price_four']=$_POST['discount_price_four'];
        $data['discount_price_five']=$_POST['discount_price_five'];


        $info = $_POST['info'];

        preg_match_all("/(font-size:)(.*?)(px;)/",$info,$str);
        foreach($str[0] as $key=>$val){
            preg_match("/(font-size:)(.*?)(px;)/",$val,$i);
            if($i[2]<20){
                $info = str_replace($i[0],"font-size:20px;",$info);
            }
        }
        if($_POST['good_id']>0){

            M("Good")->where(array("id"=>$_POST['good_id']))->save($data);
            $goodid = $_POST['good_id'];

            $info = '<meta charset="utf8"><style>  h1{ font-size:30px; } h2{ font-size:25px; } h3{ font-size:20px; } </style>'.$info;
            filewrite("./Public/static/App/goodinfo".$goodid.".html",$info);
            $this->success('修改成功');
        }else{
            $goodid = M("Good")->add($data);


            $info = '<meta charset="utf8"><style>body{font-size:20px;}  h1{ font-size:30px; } h2{ font-size:25px; } h3{ font-size:20px; } </style>'.$info;
            filewrite("./Public/static/App/goodinfo".$goodid.".html",$info);
            $this->success('新增成功');
        }
    }

}